﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GameSchool.Extra;
using System.ComponentModel.DataAnnotations;

namespace GameSchool.Models
{
	/// <summary>
	/// Course er gagnaklasi sem geymir þær upplýsingar sem þarf til að skrá ný námskeið í gagnagrunninn. 
	/// Hann heldur líka utan um hluti tengda námskeiðinu, eins og hver kennir, hverjir eru skráðir, 
	/// hvaða bók er notuð.
	/// </summary>  
	
    [Table("gs_course")]
	public class Course
    {
        public Course() 
        {
            this.Levels = new List<Level>();
            this.Teachers = new List<TeacherCourse>();
            this.Students = new List<StudentCourse>();
            this.CourseBooks = new List<CourseBook>();
        }

        [Key]
        [Column("course_id")]
        public int CourseId { get; set; }

        [Column("course_name")]
		[Display(Name = "Heiti námskeiðs")]
        [Required(ErrorMessage = "Vinsamlegast settu nafn á námskeiðið")]
        public string CourseName { get; set; }

        [Column("description")]
		[Display(Name = "Lýsing námskeiðs")]
        [Required(ErrorMessage = "Vinsamlegast settu inn lýsingu á námskeiði")]
        public string Description { get; set; }

        [NotMapped]
        public CustomTypes.CourseStateType StateType {
            get
            {
                return (CustomTypes.CourseStateType) this.StateTypeValue;
            }

            set
            {
                StateTypeValue = (Int16)value; // smallint
            }
        }

        [Column("state_type")]
        public Int16 StateTypeValue { get; set; }

        [Column("entry_date")]
        public DateTime EntryDate { get; set; }

        [Column("entry_by")]
        public int EntryBy { get; set; }

        [Column("ects")]
        public int Ects { get; set; }

        public virtual ICollection<Level> Levels { get; set; }
        public virtual ICollection<CourseBook> CourseBooks { get; set; }

        public virtual ICollection<TeacherCourse> Teachers { get; set; }
        public virtual ICollection<StudentCourse> Students { get; set; }

        public virtual ICollection<Models.Common.Notification> Notifications { get; set; }

        #region "Registrations"

        public bool RegisterStudent(Person student, int entryBy)
        {
            if (student != null)
            {
                this.Students.Add(new StudentCourse() { Course = this, Student = student, Active = true, EntryById = entryBy });
                return true;
            }
            return false;
        }

        public bool RegisterTeacher(Person teacher, int entryBy)
        {
            if (teacher != null)
            {
                this.Teachers.Add(new TeacherCourse() { Course = this, Teacher = teacher, Active = true, EntryById = entryBy});
                return true;
            }
            return false;
        }
        #endregion
    }
}